package com.kakaxi.mybilibili.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kakaxi.mybilibili.utils.DensityUtil;

/**
 * 线性间距器
 * Created by kakaxi on 16/11/14.
 */

public class LinearMarginDecoration extends RecyclerView.ItemDecoration {

    /** 默认间距 **/
    public static final int DEFAULT_MARGIN_DP = 10;

    /** 上间距 **/
    public int marginTop;
    /** 下间距 **/
    public int marginBottom;
    /** 左间距 **/
    public int marginLeft;
    /** 右间距 **/
    public int marginRight;

    /** 自动适配前后项中间的间距, 默认自动适配 **/
    private boolean mAutoFixMiddle = true;

    /** 上下文 **/
    private Context mContext;

    public LinearMarginDecoration(Context context) {
        mContext = context;
        // 默认间距
        marginTop = marginLeft = marginBottom = marginRight = DensityUtil.dipToPx(context,
                DEFAULT_MARGIN_DP);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
            state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
            if (isAutoFixMiddle()) {
                int pos = parent.getChildLayoutPosition(view);
                // 判断垂直还是水平线性布局
                if (linearLayoutManager.getOrientation() == LinearLayoutManager.VERTICAL) { // 垂直布局
                    // 第一行设置上下间距, 以下的都只有下间距
                    if (pos == 0) {
                        outRect.set(marginLeft, marginTop, marginRight, marginBottom);
                    } else {
                        outRect.set(marginLeft, 0, marginRight, marginBottom);
                    }
                } else { // 水平布局
                    // 第一行设置左右间距, 后面的都只有右间距
                    if (pos == 0) {
                        outRect.set(marginLeft, marginTop, marginRight, marginBottom);
                    } else {
                        outRect.set(0, marginTop, marginRight, marginBottom);
                    }
                }
            } else {
                // 直接进行间距设置
                outRect.set(marginLeft, marginTop, marginRight, marginBottom);
            }
        }
    }

    public boolean isAutoFixMiddle() {
        return mAutoFixMiddle;
    }

    public void setAutoFixMiddle(boolean autoFixMiddle) {
        this.mAutoFixMiddle = autoFixMiddle;
    }

    /**
     * 使用密度设置各个方向的间距
     *
     * @param marginTopDp    上间距密度
     * @param marginRightDp  右间距密度
     * @param marginBottomDp 下间距密度
     * @param marginLeftDp   左间距密度
     */
    public void setMarginDp(int marginTopDp, int marginRightDp, int marginBottomDp, int
            marginLeftDp) {
        marginTop = DensityUtil.dipToPx(mContext, marginTopDp);
        marginBottom = DensityUtil.dipToPx(mContext, marginBottomDp);
        marginLeft = DensityUtil.dipToPx(mContext, marginLeftDp);
        marginRight = DensityUtil.dipToPx(mContext, marginRightDp);
    }

    /**
     * 使用像素设置各个方向的间距
     *
     * @param marginTop    上间距
     * @param marginRight  右间距
     * @param marginBottom 下间距
     * @param marginLeft   左间距
     */
    public void setMargin(int marginTop, int marginRight, int marginBottom, int
            marginLeft) {
        this.marginTop = marginTop;
        this.marginBottom = marginBottom;
        this.marginLeft = marginLeft;
        this.marginRight = marginRight;
    }

}
